﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace WebnetUploadApp
{
	class OperateFiles
	{
		public static bool SavePWD2File(string strFile, Dictionary<string, int> pwdDict)
		{
			bool bSuc = false;
			FileStream sfs = new FileStream(strFile, FileMode.Create, FileAccess.Write);
			System.Exception errExp = null;
			if (sfs != null)
			{
				try
				{
					string strformat = @"alan";
					int nVersion = 0x00001000;
					int nperpwdLen = 12;
					int npwdNum = pwdDict.Keys.Count;
					byte[] pFormat = Encoding.Unicode.GetBytes(strformat);
					if (pFormat.Length == 8)
					{
						sfs.Write(pFormat, 0, pFormat.Length);
						byte[] pIntBuf = new Byte[4];
						pIntBuf[0] = (Byte)((nVersion & 0xFF000000) >> 24);
						pIntBuf[1] = (Byte)((nVersion & 0x00FF0000) >> 16);
						pIntBuf[2] = (Byte)((nVersion & 0x0000FF00) >> 8);
						pIntBuf[3] = (Byte)((nVersion & 0x000000FF));
						sfs.Write(pIntBuf, 0, 4);
						pIntBuf[0] = (Byte)((nperpwdLen & 0xFF000000) >> 24);
						pIntBuf[1] = (Byte)((nperpwdLen & 0x00FF0000) >> 16);
						pIntBuf[2] = (Byte)((nperpwdLen & 0x0000FF00) >> 8);
						pIntBuf[3] = (Byte)((nperpwdLen & 0x000000FF));
						sfs.Write(pIntBuf, 0, 4);
						pIntBuf[0] = (Byte)((npwdNum & 0xFF000000) >> 24);
						pIntBuf[1] = (Byte)((npwdNum & 0x00FF0000) >> 16);
						pIntBuf[2] = (Byte)((npwdNum & 0x0000FF00) >> 8);
						pIntBuf[3] = (Byte)((npwdNum & 0x000000FF));
						sfs.Write(pIntBuf, 0, 4);

						Dictionary<string, int>.KeyCollection.Enumerator emtor = pwdDict.Keys.GetEnumerator();
						while (emtor.MoveNext())
						{
							string strKey = emtor.Current;
							byte[] pKey = Encoding.Unicode.GetBytes(strKey);
							if (pKey.Length == nperpwdLen)
							{
								sfs.Write(pKey, 0, pKey.Length);
							}
							else
							{
								System.Exception exp = new System.Exception(@"PWD is not 6 Length!!");
								throw exp;
							}
						}
						emtor.Dispose();
						bSuc = true;
					}
				}
				catch (System.Exception e)
				{
					errExp = e;
					bSuc = false;
				}
				finally
				{
					sfs.Close();
					if (errExp != null)
						throw errExp;
				}
			}
			return bSuc;
		}
		public static Dictionary<string, int> ReadPWDFromFile(string strFile)
		{
			Dictionary<string, int> retDict = new Dictionary<string, int>();
			FileStream rfs = new FileStream(strFile, FileMode.Open, FileAccess.Read);
			System.Exception errExp = null;
			if (rfs != null && retDict != null)
			{
				try
				{
					Byte[] pFormat = new Byte[8];
					rfs.Read(pFormat, 0, 8);
					string strformat = Encoding.Unicode.GetString(pFormat);
					if(strformat == @"alan"){
						Byte[] pTempBuf = new Byte[4];
						rfs.Read(pTempBuf, 0, 4);
						rfs.Read(pTempBuf, 0, 4);
						int nperpwdLen = ((int)((int)pTempBuf[0]) << 24) | (int)(((int)pTempBuf[1]) << 16) | (int)(((int)pTempBuf[2]) << 8) | (int)(((int)pTempBuf[3]));
						rfs.Read(pTempBuf, 0, 4);
						int npwdNum = ((int)((int)pTempBuf[0]) << 24) | (int)(((int)pTempBuf[1]) << 16) | (int)(((int)pTempBuf[2]) << 8) | (int)(((int)pTempBuf[3]));
						int nCharsNum = nperpwdLen/2;
						for(int i = 0; i < npwdNum; i++)
						{
							Byte[] pKey = new Byte[nperpwdLen];
							rfs.Read(pKey, 0, nperpwdLen);
							string strKey = Encoding.Unicode.GetString(pKey);
							if(strKey.Length == nCharsNum){
								retDict.Add(strKey, 1);
							}
						}
					}
				}
				catch (System.Exception e)
				{
					errExp = e;
					retDict.Clear();
				}
				finally
				{
					rfs.Close();
					if (errExp != null)
						throw errExp;
				}
			}
			return retDict;
		}
	}
}
